﻿// Jonisium Experiment library
// ShowDialogOnClick() Version 1.0
// Last update Feb 23, 2012
// (c) 2011-2012 Johnny Caisip
// License: MIT (http://www.opensource.org/licenses/mit-license.php)


// This plugin will display a dialog if the event occurs
// There are multiple inputs possible in priority order: data-source-url, data-source-id, data-source-this

// Cache for dialogs
var dialogs = {};
// plugin
(function ($) {
    $.fn.showDialogOnClick = function () {
        $(this).live('click', function (e) {

            var link = $(this);
            var url = link.data('source-url');
            // id is the element id without # sign
            var source_id = link.data('source-id');
            // this is the string to display
            var source_this = link.data('source-this');
            var id = e.target;
            if (url != undefined && url.length > 0) {
                dialogUrlLoader(id, link, url);
            }
            else if (source_id != undefined && source_id.length > 0) {
                var content = $('#' + source_id).html();
                showDialog(id, link, content);
            }
            else if (source_this != undefined && source_this.length > 0) {

                showDialog(id, link, source_this);
            }

            // Prevent the normal behavior since we opened the dialog
            e.preventDefault();


             // close dialog on click to the background
            $('.ui-widget-overlay').live('click', function (e) {
                $(".ui-dialog-content").dialog("close");
            });
        });
     };
})(jQuery);

var dialogUrlLoader = function (id, link, url) {
    var separator = url.indexOf('?') >= 0 ? '&' : '?';

    // Save an empty jQuery in our cache for now.
    dialogs[id] = $();
    var rnd = Math.floor(Math.random() * 10000);
    // Load the dialog with the content=1 QueryString in order to get a PartialView
    $.get(url + separator + 'partial=1&rnd=' + rnd.toString())
            .done(function (content) {
                showDialog(id, link, content);
            });
};

var showDialog = function (id, link, content) {
    dialogs[id] = $('<div class="modal-popup">' + content + '</div>')
                    .hide() // Hide the dialog for now so we prevent flicker
                    .appendTo(document.body)
    //.filter('div') // Filter for the div tag only, script tags could surface
                    .dialog({ // Create the jQuery UI dialog
                        title: link.data('dialog-title') || 'Info',
                        modal: true,
                        resizable: true,
                        draggable: true,
                        closeOnEscape: true,
                        width: link.data('dialog-width') || 600,
                        height: link.data('dialog-height') || auto
                    })
    //  .find('form') // Attach logic on forms
    //      .submit(formSubmitHandler)
               .end();
            var hide=link.data('title-hide');
            if(hide !=undefined && hide==true) {
                 // remove the title bar
                $(".ui-dialog-titlebar").hide();
                }


};

